package com.zwb.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwb.service.mapper.DepartmentMapper;
import com.zwb.service.pojo.Department;
import com.zwb.service.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zwb
 * @since 2022-08-23
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired(required = false)
    private DepartmentMapper departmentMapper;

    /**
     * 获取所有部门信息
     *
     * @return
     */
    @Override
    public List<Department> getAllDepartments() {
        return departmentMapper.getAllDepartments(-1);
    }


    /**
     * 添加部门
     *
     * @param department
     */
    @Override
    public boolean addDepartment(Department department) {
        department.setEnabled(true);
        /* 调用存储过程 */
        departmentMapper.addDepartment(department);
        return department.getResult() == 1;
    }

    /**
     * 删除部门
     *
     * @param id
     * @return
     */
    @Override
    public Integer delDepartment(Integer id) {
        Department department = this.getById(id);
        // 不存在
        if (ObjectUtils.isEmpty(department)) {
            return 0;
        }
        department.setId(id);
        departmentMapper.delDepartment(department);
        return department.getResult();
    }
}
